import { loggerProvider } from '@app/common/constants';
import { AuthGraphqlGuard } from '@app/common/guards/auth.graphql';
import {
  BookReservation,
  Reservation,
  ReservationStatus,
} from '@app/common/types/graphqls/graphql.schema';
import { ReservationService } from '@app/services/reservation/reservation.service';
import { Get, Inject, Logger, UseGuards } from '@nestjs/common';
import { REQUEST } from '@nestjs/core';
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import { ObjectId } from 'mongoose';
@Resolver()
@UseGuards(AuthGraphqlGuard)
export class ReservationResolver {
  constructor(
    @Inject(REQUEST) private readonly request: Request,
    private readonly reservationService: ReservationService,
    @Inject(loggerProvider)
    private readonly logger: Logger,
  ) {}

  @Mutation('book')
  async book(
    @Args('reservation') reservation: BookReservation,
  ): Promise<Reservation> {
    const model = new Reservation();
    Object.assign(model, reservation);
    model.reservedBy = this.request['user'] || 'System';
    return await this.reservationService.create(model);
  }

  @Mutation('update')
  async update(
    @Args('id') id: ObjectId,
    @Args('reservation') reservation: BookReservation,
  ): Promise<Reservation> {
    const model = new Reservation();
    Object.assign(model, reservation);
    return await this.reservationService.update(id, model);
  }

  @Mutation('cancel')
  async cancel(@Args('id') id: ObjectId): Promise<Reservation> {
    return await this.reservationService.updateStatus(
      id,
      ReservationStatus.Canceled,
    );
  }

  @Mutation('update')
  async mark(
    @Args('id') id: ObjectId,
    @Args('status') status: ReservationStatus,
  ): Promise<Reservation> {
    return await this.reservationService.updateStatus(id, status);
  }

  @Get('detail')
  @Query('detail')
  async detail(@Args('id') id: ObjectId): Promise<Reservation> {
    return await this.reservationService.query(id);
  }

  @Get('browse')
  @Query('browse')
  reservation(
    @Args('arrivalTime') date: Date | string,
    @Args('status') status: ReservationStatus,
  ): Promise<Reservation[]> {
    return this.reservationService.queryBy(date, status);
  }
}
